package com.example.kill.service.impl;

import com.example.kill.dao.KillGoodsMapper;
import com.example.kill.dao.KillOrderMapper;
import com.example.kill.pojo.*;
import com.example.kill.redis.OrderKey;
import com.example.kill.redis.RedisService;
import com.example.kill.service.GoodsService;
import com.example.kill.service.KillOrderService;
import com.example.kill.service.OrderService;
import com.example.kill.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class KillOrderServiceImpl implements KillOrderService {

    @Resource
    KillOrderMapper killOrderMapper;

    @Autowired
    GoodsService goodsService;

    @Resource
    OrderService orderService;

    @Autowired
    private RedisService redisService;


    @Override
    public KillOrder getKillOrderByUserIdGoodsId(long userId, long goodsId) {

        return redisService.get(OrderKey.getKillOrderByUidGid,""+userId+""+goodsId,KillOrder.class);
    }

    /**
     * //减库存 下订单 写入秒杀订单
     * @param user
     * @param goodsVo
     * @return
     */
    @Transactional
    public KillOrderInfo kill(KillUser user, GoodsVo goodsVo) {
        // 1 减库存
        goodsService.reduceStock(goodsVo);

        return   orderService.createOrder(user,goodsVo);


    }
}
